<?php
/**
 * alltosun.com  store_move.php
 * ============================================================================
 * 版权所有 (C) 2009-2013 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 王德康 (wangdk@alltosun.com) $
 * $Date: 2016-4-25 下午3:06:41 $
 * $Id$
 */

class Action
{
    private $per_page = 100;

    public function __call($action = '', $params = array())
    {

        $search_filter = Request::getParam('search_filter', array());
        $page          = Request::getParam('page_no', 1);
        $filter['is_delete'] = 1;


        $filter = array();
        if (isset($search_filter['mobile']) && $search_filter['mobile']) {

            $user_info = user_helper::get_user_info_by_mobile($search_filter['mobile']);
            if ($user_info) {
                // 合伙人
                $filter['agency_id'] = $user_info['id'];
            }
        }

        if (isset($search_filter['store_mobile']) && $search_filter['store_mobile']) {

            $user_info = user_helper::get_user_info_by_mobile($search_filter['store_mobile']);
            if ($user_info) {
                // 合伙人
                $filter['user_id'] = $user_info['id'];
            }
        }

        if ((isset($filter['agency_id']) && $filter['agency_id']) ||  (isset($filter['user_id']) && $filter['user_id']) ) {
            $store_list = get_data_list('user_store', $filter, ' order BY `id` DESC ', $page, $this->per_page);
            if ($store_list) {
                foreach ($store_list as $k => $v) {
                    $store_list[$k]['user_info']        = user_helper::get_user_info($v['user_id']);
                    $store_list[$k]['agency_user_info'] = user_helper::get_user_info($v['agency_id']);
                    $store_list[$k]['market_user_info'] = user_helper::get_user_info($v['marketing_id']);
                }
            }
            Response::assign('store_list', $store_list);
        }

        Response::assign('search_filter', $search_filter);
        Response::display('admin/store_move/search_list.html');
    }

    /**
     * 跨合伙人迁移门店
     * @return multitype:string
     */
    public function save_agency()
    {
        $desc_mobile = Request::post('desc_mobile', '');
        $owner_ids   = Request::post('owner_ids', array());

        if (!$owner_ids) {
            return '请选择要迁移的门店!';
        }

        $desc_user_info = user_helper::get_user_info_by_mobile($desc_mobile);
        if (!$desc_user_info) {
            return '迁移失败，请填写有效的合伙人手机号!';
        }

        $role_id = user_helper::get_user_role_id($desc_user_info['id']);
        if ($role_id != user_config::AGENCY) {
            return '迁移失败，迁移合伙人角色错误!';
        }

        $market_id = $desc_user_info['id'];
        $agency_id = $desc_user_info['id'];

        $result = _widget('dealers')->market_store_to_other($owner_ids, $market_id, $agency_id);
        if ($result[0]) {
            // if (ONDEV) store_helper::update_store_goods_price($owner_ids);
            $msg = '成功迁移'.$result[0].'家门店';
            return array('info'=>'ok', 'msg'=>$msg);
        }
        return array('info'=>$result[1]);
    }
}
